package study.ai.langchain4j.controller;

import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import study.ai.langchain4j.service.ChatAssistant;
import study.ai.langchain4j.service.ChatStreamAssistant;

import java.util.List;

@Slf4j
@RequestMapping("/chat")
@RestController
public class ChatController {

    @Autowired
    private ChatLanguageModel chatLanguageModel;
    @Autowired
    private ChatAssistant chatAssistant;
    @Autowired
    private ChatStreamAssistant chatStreamAssistant;

    @RequestMapping("/low")
    public String low(String message) {
        ChatResponse response = chatLanguageModel.chat(UserMessage.from(message));
        String text = response.aiMessage().text();
        return text;
    }

    @RequestMapping("/high")
    public String high(String message) {
        return chatAssistant.chat(message);
    }

    @RequestMapping("/role/low")
    public String role(String message) {
        ChatResponse response = chatLanguageModel.chat(List.of(
                SystemMessage.systemMessage("假如你是特朗普"),
                UserMessage.from(message))
        );
        String text = response.aiMessage().text();
        return text;
    }

    @RequestMapping("/role/high")
    public String rolehigh(String message) {
        return chatAssistant.chatByRole(message);
    }

    @RequestMapping("/stream/high")
    public Flux<String> stream(@RequestParam("message") String message) {
        return chatStreamAssistant.chat(message);
    }
}
